package com.tiantian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tiantian.bean.PageBean;
import com.tiantian.dao.BuyersUsersRepository;
import com.tiantian.datajpa.service.impl.GenericServiceImpl;
import com.tiantian.domain.Buyersusers;
import com.tiantian.service.BuyersUsersService;
import com.tiantian.util.CommonConstant.QueryType;
import com.tiantian.util.ConditionResult;

/**
 * @description 审核管理
 */
@Service
@Transactional
public class BuyersUsersServiceImpl extends GenericServiceImpl<Buyersusers, String> implements BuyersUsersService {
	private BuyersUsersRepository repository;

	@Autowired
	public void setRepository(BuyersUsersRepository repository) {
		super.setGenericDAO(repository);
		this.repository = repository;
	}

	/**
	 * 获取买手用户列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 */
	public List<Buyersusers> list(Map<String, String> params, PageBean pageBean) {
		ConditionResult conditionResult = getQueryCondition(params,
				QueryType.LIST);
		List<Buyersusers> ret = repository.queryListBySql(
				conditionResult.getSql(), pageBean.getStartIndex(),
				pageBean.getPageSize(), conditionResult.getParams(),
				Buyersusers.class);
		conditionResult = getQueryCondition(params, QueryType.COUNT);
		pageBean.setTotal(repository.queryCountBySql(conditionResult.getSql(),
				conditionResult.getParams()));
		return ret;
	}

	/**
	 * 封装查询语句
	 * 
	 * @param searchMap
	 * @param type
	 * @return
	 */
	private ConditionResult getQueryCondition(Map<String, String> searchMap,
			QueryType type) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		switch (type) {
		case LIST:
			sql.append(" select t.* ");

			break;
		case COUNT:
			sql.append(" select count(1)");
			break;
		default:
			break;
		}
		sql.append(" from buyersusers t where 1=1 ");

		if (searchMap != null) {

			String buyersUserID = searchMap.get("buyersUserID");
			if (StringUtils.isNotBlank(buyersUserID)) {
				sql.append(" and buyersUserID like :buyersUserID ");
				params.put("buyersUserID", '%' + buyersUserID.trim() + '%');
			}

			// 商家登陆账号
			String userName = searchMap.get("userName");
			if (StringUtils.isNotBlank(userName)) {
				sql.append(" and userName like :userName ");
				params.put("userName", '%' + userName.trim() + '%');
			}

			// 账号状态
			String accountStatus = searchMap.get("accountStatus");
			if (StringUtils.isNotBlank(accountStatus)) {
				sql.append(" and accountStatus=:accountStatus ");
				params.put("accountStatus", accountStatus.trim());
			}
			// 审核申请状态
			String phone = searchMap.get("phone");
			if (StringUtils.isNotBlank(phone)) {
				sql.append(" and phone like :phone ");
				params.put("phone", '%' + phone.trim() + '%');
			}
			// 联系QQ
			String qqNo = searchMap.get("qqNo");
			if (StringUtils.isNotBlank(qqNo)) {
				sql.append(" and qqno like :qqNo ");
				params.put("qqNo", '%' + qqNo.trim() + '%');
			}

			// 联系微信
			String wechatNo = searchMap.get("wechatNo");
			if (StringUtils.isNotBlank(wechatNo)) {
				sql.append(" and wechatNo like :wechatNo ");
				params.put("wechatNo", '%' + wechatNo.trim() + '%');
			}
			String fullName = searchMap.get("fullName");
			if (StringUtils.isNotBlank(fullName)) {
				sql.append(" and fullName like :fullName ");
				params.put("fullName", '%' + fullName.trim() + '%');
			}

			// 开始时间
			String startRegistrationTime = searchMap
					.get("startRegistrationTime");
			if (StringUtils.isNotBlank(startRegistrationTime)) {
				sql.append(" and registrationTime>=:startRegistrationTime ");
				params.put("startRegistrationTime",
						startRegistrationTime.trim());
			}
			// 结束时间
			String endRegistrationTime = searchMap.get("endRegistrationTime");
			if (StringUtils.isNotBlank(endRegistrationTime)) {
				sql.append(" and registrationTime<=:endRegistrationTime ");
				params.put("endRegistrationTime", endRegistrationTime.trim());
			}
		}
		if (type == QueryType.LIST) {
			sql.append(" order by t.registrationTime desc");
		}
		return new ConditionResult(sql.toString(), params);

	}

	public Buyersusers getByUserName(String userName) {
		String sql = " select * from buyersusers where username=:userName";
		Map<String, Object> param = new HashMap<>();
		param.put("userName", userName);
		List<Buyersusers> users = this.repository.queryListBySql(sql, param,
				Buyersusers.class);
		if (users != null && users.size() > 0)
			return users.get(0);
		return null;

	}

}
